package com.cipriancaba.slideshow
{
	import com.adobe.utils.XMLUtil;
	import com.adobe.xml.syndication.rss.Item20;
	import com.adobe.xml.syndication.rss.RSS20;
	import com.cipriancaba.loaders.XMLLoader;
	import com.cipriancaba.events.XMLEvent;
	
	import flash.events.Event;
	
	import mx.containers.Canvas;
		
	public class SlideshowLogic extends Canvas
	{
		private var __loader:com.cipriancaba.loaders.XMLLoader;
		private var __loaderURL:String;
		
		
		public function loadXML(url:String):void {
			url = "http://picasaweb.google.com/data/feed/base/user/caba.ciprian/albumid/5160635410806488785?kind=photo&alt=rss&hl=en_US";
			__loaderURL = url;
			__loader = new XMLLoader(__loaderURL);
			__loader.addEventListener("loadComplete", onXMLLoaded);
		}
		
		private function onXMLLoaded(event:XMLEvent):void {
			/* if (__loaderURL.indexOf("picasaweb") > -1) {
				parsePicasa(event.xml);
			}
			else if (__loaderURL.indexOf("flickr") > -1) {
				parseFlickr(event.xml);
			} */
			parseRSS(String(event.xml));
		}
		
		//parses RSS 2.0 feed and prints out the feed titles into
		//the text area
		private function parseRSS(data:String):void {
			//XMLSyndicationLibrary does not validate that the data contains valid
			//XML, so you need to validate that the data is valid XML.
			//We use the XMLUtil.isValidXML API from the corelib library.
			if(!XMLUtil.isValidXML(data)) {
				trace("Feed does not contain valid XML.");
				return;
			}	
		
			//create RSS20 instance
			var rss:RSS20 = new RSS20(); 
		
			//parse the raw rss data
			rss.parse(data);
	
			//get all of the items within the feed
			var items:Array = rss.items;
	
			//loop through each item in the feed
			for each(var item:Item20 in items) {
				//print out the title of each item
				/* var i:XML;
	            for each(i in item.mediaObject.thumbnail) {
	                trace("item: " + i.toXMLString());
	            } */
	
				trace(item.mediaObject.content.@url);
			}
		}
		
		private function parsePicasa(xml:XML):void {

		}
		
		private function parseFlickr(xml:XML):void {
			
		}
	}
}